<?php

namespace App\Common\Models;

use App\Admin\Models\AdminPaytypeList;
use App\Admin\Models\AdminUser;
use App\Admin\Models\SiteCompany;
use App\Admin\Models\SiteStore;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Common;
use App\Common\Services\Admin\Common as AdminCommon;
use App\Common\Services\Store\Common as StoreCommon;

class StoreRenewalLog extends Model
{
    protected $table = 'site_store_renewal_log';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getListByAdmin(Request $request)
    {
        $query = self::with(['companyinfo','adminuserinfo','storeinfo','paytypeinfo']);


        // $info=AdminCommon::getCurrentAdmininfo($request);
        // if($info['type']!=0){//运维只可查看自己
        //     $query = $query->where(['admin_user_id'=>$info['id']]);
        // }

        $is_confirm = $request->is_confirm ?? 0;
        $isgetall = intval($request->isgetall) ?? 0;
        if (in_array($is_confirm, [1, 2])) {
            $query->where('is_confirm', $is_confirm);
        }

        $store_name = $request->store_name;
        if ($store_name != '') {
            $store_ids=SiteStore::where('store_name', 'like', '%' . $store_name . '%')->pluck('id');
            $query->whereIn('store_id', $store_ids);
        }

        $company_name = $request->company_name;
        if ($company_name != '') {
            $cp_ids=SiteCompany::where('company_name', 'like', '%' . $company_name . '%')->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }
        $pay_type = $request->pay_type ?? 0;
        if ($pay_type>0) {
            $query->where('pay_type', $pay_type);
        }

        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('pay_at', '>=', $start_time)->where('pay_at', '<=', $end_time);
        }
        $query ->orderBy('pay_at', 'desc');
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public static function getListByStore(Request $request)
    {
        $cp_id=StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::where(['cp_id'=>$cp_id])->with(['companyinfo','adminuserinfo','storeinfo','paytypeinfo'])->orderBy('create_at', 'desc');



        $is_confirm = $request->is_confirm ?? 0;
        $isgetall = intval($request->isgetall) ?? 0;
        if (in_array($is_confirm, [1, 2])) {
            $query->where('is_confirm', $is_confirm);
        }

        $store_name = $request->store_name;
        if ($store_name != '') {
            $store_ids=SiteStore::where('store_name', 'like', '%' . $store_name . '%')->pluck('id');
            $query->whereIn('store_id', $store_ids);
        }

        $company_name = $request->company_name;
        if ($company_name != '') {
            $cp_ids=SiteCompany::where('company_name', 'like', '%' . $company_name . '%')->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }
        $pay_type = $request->pay_type ?? 0;
        if ($pay_type>0) {
            $query->where('pay_type', $pay_type);
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function companyinfo(){
        return $this->hasOne(SiteCompany::class, 'id', 'cp_id');
    }

    public function adminuserinfo(){
        return $this->hasOne(AdminUser::class, 'id', 'admin_user_id');
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }

    public function paytypeinfo(){
        return $this->hasOne(AdminPaytypeList::class, 'id', 'pay_type');
    }
}
